package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.Result;
import com.example.entity.dto.HandoverlogInsertDto;
import com.example.entity.yifei.Handoverlog;
import com.example.service.HandoverlogService;
import com.example.untils.BeanUtils;
import com.example.untils.SecurityUtils;
import com.example.untils.TimeUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 交接日志 前端控制器
 * </p>
 *
 * @author Lan
 * @since 2024-09-25
 */
@RestController
@RequestMapping("/api/handoverlog")
public class HandoverlogController {

    @Autowired
    private HandoverlogService service;

    @ApiOperation("通过机台编号获取交接记录")
    @GetMapping("loglist")
    public Result<List<Handoverlog>> loglist(@RequestParam String macNo, @RequestParam Integer type) {
        return Result.success(service.list(new LambdaQueryWrapper<Handoverlog>().eq(Handoverlog::getMacNo, macNo).eq(Handoverlog::getType, type)));
    }

    @ApiOperation("标记为已读")
    @GetMapping("read")
    public Result read(@RequestParam String uuid) {
        Handoverlog log = new Handoverlog();  log.setReader(SecurityUtils.getLoginUser().getUsername());   log.setReadertime(TimeUtils.getFullNowTime());
        log.setStatus(1);
        service.update(log, new LambdaQueryWrapper<Handoverlog>().eq(Handoverlog::getUuid, uuid));
        return Result.success();
    }

    @ApiOperation("标记为已完成")
    @GetMapping("finished")
    public Result finished(@RequestParam String uuid) {
        Handoverlog log = new Handoverlog();  log.setFinisher(SecurityUtils.getLoginUser().getUsername());   log.setFinisherTime(TimeUtils.getFullNowTime());
        log.setStatus(2);
        service.update(log, new LambdaQueryWrapper<Handoverlog>().eq(Handoverlog::getUuid, uuid));
        return Result.success();
    }

    @ApiOperation("新增交接记录")
    @PostMapping("handoverlogInsert")
    public Result handoverlogInsert(@RequestBody HandoverlogInsertDto dto) {
        Handoverlog log = new Handoverlog();
        BeanUtils.copyPropertiesIgnoreCase(dto, log);
        log.setCreator(SecurityUtils.getLoginUser().getUsername());
        log.setCreatetime(TimeUtils.getFullNowTime());
        log.setUuid(UUID.randomUUID().toString());
        log.setStatus(0);
        service.save(log);
        return Result.success();
    }

}
